package bTree.LeetCode;

public class 打家劫舍3 {
    public int rob(TreeNode root) {
        int []dp=robTree(root);
        return Math.max(dp[0],dp[1]);
    }

    private int[] robTree(TreeNode root) {
        if(root==null){
            return new int[]{0, 0};
        }
        int[]left=robTree(root.left);
        int[]right=robTree(root.right);
        //偷当前节点
        int []dp=new int[2];
        dp[1]=root.val+left[0]+right[0];
        //不偷当前节点
        dp[0]=Math.max(left[0],left[1] )+Math.max(right[0],right[1]);
        return dp;
    }

}